﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/06/16
 * 
 * 功能说明：功能模块模板添加和修改页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_CommonModel_ModelTemplate : System.Web.UI.Page
{
    protected int ModelID = 0;
    protected string ModelName = "";
    protected string Action = "";
    protected int ModelTemplateID = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        ModelID = NetRequest.GetQueryInt("ModelID");
        ModelTemplateID = NetRequest.GetQueryInt("ModelTemplateID");
        ModelName = Server.UrlDecode(NetRequest.GetQueryString("ModelName"));
        Action = NetRequest.GetQueryString("Action");

        if (!IsPostBack)
        {
            switch (Action)
            {
                case "Add": break;
                case "Modify":
                    BindData();
                    break;
                case "Delete":
                    BindData();
                    btnSumbit.Attributes.Add("onclick", "return confirm('确定要删除吗？');");
                    break;
            }
        }
    }

    private void BindData()
    {
        Whir.BLL.Whir_ModelTemplate bll = new Whir.BLL.Whir_ModelTemplate();
        Whir.Model.Whir_ModelTemplate model = bll.GetModel(ModelTemplateID);

        txtModelTemplateName.Text = model.TemplateName;
        txtModelTemplateDesc.Text = model.TemplateDesc;
    }

    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        Whir.BLL.Whir_ModelTemplate bll = new Whir.BLL.Whir_ModelTemplate();
        Whir.Model.Whir_ModelTemplate model;
        switch (Action)
        {
            case "Add":
                model = new Whir.Model.Whir_ModelTemplate();
                model.TemplateName = txtModelTemplateName.Text.Trim();
                model.TemplateDesc = txtModelTemplateDesc.Text.Trim();
                model.TemplateFields = GetFieldsList(ModelID);
                bll.Add(model);
                break;
            case "Modify":
                model = bll.GetModel(ModelTemplateID);
                model.TemplateName = txtModelTemplateName.Text.Trim();
                model.TemplateDesc = txtModelTemplateDesc.Text.Trim();
                bll.Update(model);
                break;
            case "Delete":
                model = bll.GetModel(ModelTemplateID);
                if (model != null)
                {
                    bll.Delete(ModelTemplateID);
                }
                break;
        }

        Response.Redirect("ModelTemplateManage.aspx");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ModelTemplateManage.aspx");
    }

    protected string GetFieldsList(int modelID)
    {
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        DataTable dt = bll.GetList(" ModelID=" + modelID).Tables[0];
        string temp = "";

        temp += modelID + "|";
        foreach (DataRow row in dt.Rows)
        {
            temp += row["fieldName"].ToString() + ",";
        }
        temp = temp.TrimEnd(',');

        return temp;
    }
}
